﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IAC_2013.com.iac.form;
using IAC_2013.com.iac.util;
using System.Data.SqlClient;
using System.Data;
using DevComponents.DotNetBar.Controls;

namespace IAC_2013.com.iac.action
{
    class PartnersAction
    {
        static SqlConnection conn = new SqlConnection(DBUtils.CONNECTION_STRING);
        static SqlCommand cmd;
        static SqlDataAdapter da;
        static DataTable tb = null;
        static DataGridViewX grv = null;
        public Boolean addPartners(PartnerForm frm)
        {
            String sql = "INSERT INTO ncc_nha_cung_cap VALUES(";
            sql += "'" + frm.partnerCode +"', ";
            sql += "N'"+ frm.partnerName +"', ";
            sql += "N'"+ frm.partnerAddress +"', ";
            sql += "'"+ frm.partnerPhone +"', ";
            sql += "'"+ frm.partnerFax +"', ";
            sql += "'"+ frm.partnerEmail +"', ";
            sql += "'"+ frm.partnerWebsite +"'";
            sql += ")";
            try
            {
                conn.Open();
                cmd = new SqlCommand();
                cmd.CommandText = sql;
                cmd.Connection = conn;
                cmd.ExecuteNonQuery();
                conn.Close();
                cmd.Dispose();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean updatePartner(PartnerForm frm)
        {
            String sql = "UPDATE ncc_nha_cung_cap SET ";
            sql += " ma_ncc = '" + frm.partnerCode + "',";
            sql += " ten_ncc = N'" + frm.partnerName + "', ";
            sql += " diachi_ncc = N'" + frm.partnerAddress + "', ";
            sql += " dien_thoai_ncc = '" + frm.partnerPhone + "', ";
            sql += " fax_ncc = '" + frm.partnerFax + "', ";
            sql += " email_ncc = '" + frm.partnerEmail + "', ";
            sql += " website_ncc = '" + frm.partnerWebsite + "'";
            sql += " WHERE ";
            sql += " id_ncc = '" + frm.partnerId + "'";
            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataGridViewX loadPartners()
        {
            String sql = "SELECT * FROM ncc_nha_cung_cap ncc ORDER BY ncc.ten_ncc DESC ";

            try
            {
                cmd = new SqlCommand();
                cmd.CommandText = sql;
                cmd.Connection = conn;
                conn.Open();
                da = new SqlDataAdapter(cmd);
                tb = new DataTable();
                da.Fill(tb);

                grv = new DataGridViewX();
                grv.DataSource = tb;
                conn.Close();

                return grv;
            }
            catch (Exception)
            {
                return grv;
            }
        }

        public static DataTable listPartner(int partnerId)
        {
            tb = new DataTable();
            try
            {
                String sql = "select * from ncc_nha_cung_cap where 1 = 1 ";
                if (partnerId != 0)
                {
                    sql += " and id_ncc = " + partnerId;
                }
                sql += " order by id_ncc desc";
                cmd = new SqlCommand(sql, conn);
                conn.Open();

                da = new SqlDataAdapter(cmd);
                da.Fill(tb);

                conn.Close();
                return tb;
            }
            catch (Exception)
            {
                conn.Close();
                return tb;
            }
        }

    }
}
